package com.shanzmoo.platform.manage.api.manager.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.db.module.product.domain.query.SkuQueryVO;
import com.shanzmoo.db.module.product.domain.vo.ProductVO;
import com.shanzmoo.db.module.product.domain.vo.SkuResultVO;
import com.shanzmoo.db.module.product.service.IProductSkuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品skumanager
 * @author xueqz
 */
@Component
@RequiredArgsConstructor
public class ProductSkuManager {

    @Autowired
    private IProductSkuService productSkuService;

    public SHttpResponse<PageRespBase<SkuResultVO>> getSkuList(SkuQueryVO skuQueryVO) {
        IPage<SkuResultVO> skuResultVOIPage = productSkuService.getSkuList(skuQueryVO);
        for (SkuResultVO skuResultVO : skuResultVOIPage.getRecords()) {
            if(StringUtils.isNotBlank(skuResultVO.getCategoryName1())){
                String categoryNames = skuResultVO.getCategoryName1();
                String[] names = categoryNames.split(",");
                if(names.length != 3) {
                    SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYIDLENGTH, "错误商品分类级别:"+names.length);
                }
                skuResultVO.setCategoryName1(names[0]);
                skuResultVO.setCategoryName2(names[1]);
                skuResultVO.setCategoryName3(names[2]);
            }else{
                SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYNAME, "商品分类名未找到");
            }
        }
        PageRespBase<SkuResultVO> re = PageRespBase.create(skuResultVOIPage, skuResultVOIPage.getRecords());
        return SHttpResponse.ok(re);
    }
}
